﻿using System;
using System.Linq;
using System.Drawing;

namespace Spencen.Mobile.UI.Primitives
{
    public abstract class PolygonElement : DrawingElement
    {
        protected abstract Point[] GeneratePoints();

        public override bool HitTest( Point hitPoint, GraphicsContext context )
        {
            return DrawingSupport.PolygonContains( GeneratePoints(), hitPoint );
        }

        protected override void OnRender( GraphicsContext context )
        {
            var points = GeneratePoints();

            points = context.TransformedPoints( points ); ;

            if ( Background != null )
                context.Graphics.FillPolygon( Background, points );
            if ( Stroke != null )
                context.Graphics.DrawPolygon( Stroke, points );
        }

        public override Rectangle TransformedBounds(GraphicsContext context)
        {
            var transformedPoints = context.TransformedPoints( GeneratePoints() );
            var minX = transformedPoints.Min( p => p.X );
            var minY = transformedPoints.Min( p => p.Y );
            var maxX = transformedPoints.Max( p => p.X );
            var maxY = transformedPoints.Max( p => p.Y );

            var bounds = new Rectangle( minX, minY, maxX - minX, maxY - minY );

            return bounds;
        }
    }
}
